package util.old;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


/**
 * Servlet implementation class UploadImgServlet
 */
@WebServlet("/util.old.Uploadpic")
public class Uploadpic extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	protected void service(HttpServletRequest request, HttpServletResponse response)
		    throws ServletException, IOException {
		// �ж��ϴ����Ƿ�Ϊmultipart/form-data����
		HttpSession session = request.getSession();
		//String username = session.getAttribute("username"); // �ڵ�¼ʱ�� User ��������� �Ự
								 // ��
	 
		if (ServletFileUpload.isMultipartContent(request)) {
	 
		    try {
			// 1. ����DiskFileItemFactory�������û�������С����ʱ�ļ�Ŀ¼
			DiskFileItemFactory factory = new DiskFileItemFactory();
			// System.out.println(System.getProperty("java.io.tmpdir"));//Ĭ����ʱ�ļ���
	 
			// 2. ����ServletFileUpload���󣬲������ϴ��ļ��Ĵ�С���ơ�
			ServletFileUpload sfu = new ServletFileUpload(factory);
			sfu.setSizeMax(10 * 1024 * 1024);// ��byteΪ��λ ���ܳ���10M 1024byte =
							 // 1kb 1024kb=1M 1024M = 1G
			sfu.setHeaderEncoding("utf-8");
	 
			// 3.
			// ����ServletFileUpload.parseRequest��������request���󣬵õ�һ�������������ϴ����ݵ�List����
			@SuppressWarnings("unchecked")
			List<FileItem> fileItemList = sfu.parseRequest(request);
			Iterator<FileItem> fileItems = fileItemList.iterator();
	 
			// 4. ����list��ÿ����һ��FileItem���󣬵�����isFormField�����ж��Ƿ����ϴ��ļ�
			while (fileItems.hasNext()) {
			    FileItem fileItem = fileItems.next();
			    // ��ͨ��Ԫ��
			    if (fileItem.isFormField()) {
				String name = fileItem.getFieldName();// name����ֵ
				String value = fileItem.getString("utf-8");// name��Ӧ��valueֵ
	 
				System.out.println(name + " = " + value);
			    }
			    // <input type="file">���ϴ��ļ���Ԫ��
			    else {
				String fileName = fileItem.getName();// �ļ�����
				//System.out.println("ԭ�ļ�����" + fileName);// Koala.jpg
	 
				String suffix = fileName.substring(fileName.lastIndexOf('.'));
				//System.out.println("��չ����" + suffix);// .jpg
	 
				// ���ļ�����Ψһ��
				String newFileName = new Date().getTime() + suffix;
				//System.out.println("���ļ�����" + newFileName);// image\1478509873038.jpg
				String user_img = "img/"+newFileName;//�������ݿ�url
				
				
				// 5. ����FileItem��write()������д���ļ�
				newFileName = "1.jpg";
				File file = new File("D:/" + newFileName);//·���ĳ��Լ����������ͼƬ��·��������Ǳ�����ֱ��д����·����Զ�̷������Ļ�����о�һ��ftp��Э�飬�������һ��������Ҫ��һ�������Զ�̽ӿڵ���
				System.out.println(file.getAbsolutePath());
				fileItem.write(file);
	 
				// 6. ����FileItem��delete()������ɾ����ʱ�ļ�
				fileItem.delete();
				
			    }
			}
	 
		    } catch (FileUploadException e) {
			e.printStackTrace();
		    } catch (Exception e) {
			e.printStackTrace();
		    }
		    
		    
		    /////////////////// ͼƬʶ�𲢻ش� ////////////////////////////
		    //System.out.println(System.getProperty("user.dir"));
		    System.out.println(Test.exec("python yolov5/detect.py --source D:/1.jpg --weight yolov5/yolov5s.pt"));
		    
			response.setContentType("image/jpeg");
			File f=new File("C:/Users/1575280002/Desktop/runs/detect/exp/1.jpg");
			BufferedImage bi=ImageIO.read(f);
			OutputStream os=response.getOutputStream();
			ImageIO.write(bi, "jpg", os);
			
			File file = new File("C:\\Users\\1575280002\\Desktop\\runs");
	        System.out.println("result��"+ DelFiles.delFiles(file));
			}
		}
}
 
